﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace ykglxt
{
    public partial class Form_YPRKD : Form
    {
        int rkd;
        public Form_YPRKD(int rkd)
        {
            InitializeComponent();
            this.rkd = rkd;
        }

        SqlConnection con;
        SqlDataAdapter sda;
        DataSet ds;
        BindingSource bs;
        SqlCommandBuilder builder;
        string constr;

        private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
        {
            dgvYPRKD.ClearSelection();
            dgvYPRKD.Rows[0].Selected = true;
            dgvYPRKD.FirstDisplayedScrollingRowIndex = 0;
        }

        private void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e)
        {
            int rowindx = (dgvYPRKD.SelectedRows[0].Index - 1 + dgvYPRKD.RowCount) % dgvYPRKD.RowCount;
            dgvYPRKD.ClearSelection();
            dgvYPRKD.Rows[rowindx].Selected = true;
            dgvYPRKD.FirstDisplayedScrollingRowIndex = dgvYPRKD.SelectedRows[0].Index;
        }

        private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e)
        {
            int rowIndex = (dgvYPRKD.SelectedRows[0].Index + 1) % dgvYPRKD.RowCount;
            dgvYPRKD.ClearSelection();
            dgvYPRKD.Rows[rowIndex].Selected = true;
            dgvYPRKD.FirstDisplayedScrollingRowIndex = rowIndex;
            if (rowIndex < dgvYPRKD.FirstDisplayedScrollingRowIndex)
            {
                dgvYPRKD.FirstDisplayedScrollingRowIndex = rowIndex;
            }
            else if (!dgvYPRKD.Rows[rowIndex].Displayed)
            {
                dgvYPRKD.FirstDisplayedScrollingRowIndex++;
            }
        }

        private void bindingNavigatorMoveLastItem_Click(object sender, EventArgs e)
        {
            dgvYPRKD.ClearSelection();
            dgvYPRKD.Rows[dgvYPRKD.RowCount - 2].Selected = true;
            dgvYPRKD.FirstDisplayedScrollingRowIndex = dgvYPRKD.RowCount - 1;
        }

        private void Form_YPRKD_Load(object sender, EventArgs e)
        {
            #region 初始化
            //批准文号
            string strWH = "SELECT * FROM Dic_PZWH";
            con = new SqlConnection(PublicUnit.ConnetionString);
            DataTable dtWH = new DataTable();
            sda = new SqlDataAdapter(strWH, con);
            sda.Fill(dtWH);
            cbWH.DataSource = dtWH;
            cbWH.DisplayMember = "PZWH";
            cbWH.ValueMember = "YPBM";
            //采购员
            string strCGY = "SELECT * FROM Dic_RY";
            DataTable dtCGY = new DataTable();
            sda = new SqlDataAdapter(strCGY, con);
            sda.Fill(dtCGY);
            cbCGY.DataSource = dtCGY;
            cbCGY.DisplayMember = "MC";
            cbCGY.ValueMember = "BM";
            //生产厂商
            string strSCCJ = "SELECT * FROM Dic_SCS";
            DataTable dtSCCJ = new DataTable();
            sda = new SqlDataAdapter(strSCCJ, con);
            sda.Fill(dtSCCJ);
            cbSCCJ.DataSource = dtSCCJ;
            cbSCCJ.DisplayMember = "MC";
            cbSCCJ.ValueMember = "BM";
            //入库类型
            string strRKLX = "SELECT * FROM Dic_CRLX";
            DataTable dtRKLX = new DataTable();
            sda = new SqlDataAdapter(strRKLX, con);
            sda.Fill(dtRKLX);
            cbRKLX.DataSource = dtRKLX;
            cbRKLX.DisplayMember = "MC";
            cbRKLX.ValueMember = "BM";
            #endregion
            tbDH.Text = Convert.ToString(rkd);
            //录入人员
            tbLRRY.Text = PublicUnit.UserName;
            //
            con = new SqlConnection(PublicUnit.ConnetionString);
            constr = "SELECT YPBM,(SELECT YP.MC FROM Dic_YP YP,ML_RKD1 RKD1 WHERE RKD1.YPBM = YP.BM AND RKD1.RKDH = ";
            constr += rkd + "),BZSL,FLAG,SL,JJ,PJ,LSJ,PH,FPH FROM ML_RKD1 WHERE RKDH = ";
            constr += rkd ;
            //(SELECT YP.MC FROM Dic_YP YP,ML_RKD1 RKD1 WHERE RKD1.YPBM = YP.BM AND RKD1.RKDH = 1) as mc
            //constr = "SELECT YPBM,JJ,PJ,BZSL,SL,JJ,PJ,LSJ,PH,FPH FROM ML_RKD1 WHERE RKDH = ";
            //constr += rkd ;
            //constr = "SELECT YPBM,JJJE FROM ML_RKD1 WHERE RKDH = 1";
            sda = new SqlDataAdapter(constr , con);
            builder = new SqlCommandBuilder(sda);
            ds = new DataSet();
            bs = new BindingSource();
            dgvYPRKD.AutoGenerateColumns = true;
            try
            {
                sda.Fill(ds);
                dgvYPRKD.DataSource = ds.Tables[0];
                dgvYPRKD.Columns[0].HeaderText = "编码";
                dgvYPRKD.Columns[1].HeaderText = "名称";
                dgvYPRKD.Columns[2].HeaderText = "剂量";
                dgvYPRKD.Columns[3].HeaderText = "包装";
                dgvYPRKD.Columns[4].HeaderText = "数量";
                dgvYPRKD.Columns[5].HeaderText = "进价";
                dgvYPRKD.Columns[6].HeaderText = "批价";
                dgvYPRKD.Columns[7].HeaderText = "零价";
                dgvYPRKD.Columns[8].HeaderText = "批号";
                dgvYPRKD.Columns[9].HeaderText = "发票号";
                bs.DataSource = ds.Tables[0];
                bNavigator.BindingSource = bs;
                dgvYPRKD.Rows[0].Selected = true;
            }
            catch (SqlException ex)
            {
                if (ex.Number == 18456)
                    MessageBox.Show("网络故障，请稍后再试！", "警告" + ex.Number, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {

        }

        private void bindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            try
            {
                con = new SqlConnection(PublicUnit.ConnetionString);
                string sSave = "INSERT INTO [ML_RKD1] (" +
                    "RKDH, YPBM, BZSL, PH, PZWH," +            //1
                    "SL, RLL, JJ, PJ, LSJ, GYL," +             //2
                    //"FPRQ, FPH, SCRQ, SCSBM, YXRQ, ZCSB," +    //4
                    "YSJL, HWBM, SCXKZ, WSXKZ," +              //5
                    "GYSBM, BZ,  YPKC) VALUES(" +         //OTHER
                    //------------------------------------------------
                    "@RKDH, @YPBM, @BZSL, @PH, @PZWH," +            //1
                    "@SL, @RLL, @JJ, @PJ, @LSJ, @GYL," +            //2
                    //"@FPRQ, @FPH, @SCRQ, @SCSBM, @YXRQ, @ZCSB," +   //4
                    "@YSJL, @HWBM, @SCXKZ, @WSXKZ," +               //5
                    "@GYSBM, @BZ,  @YPKC) ";                  //OTHER
                con.Open();
                SqlCommand cmdInsert = new SqlCommand(sSave, con);
                cmdInsert.Parameters.AddWithValue("@RKDH", rkd);
                cmdInsert.Parameters.AddWithValue("@YPBM", tbBM.Text);
                cmdInsert.Parameters.AddWithValue("@BZSL", tbYPBZ.Text);
                cmdInsert.Parameters.AddWithValue("@PH", tbPH.Text);
                cmdInsert.Parameters.AddWithValue("@PZWH", cbWH.Text);
                //---
                cmdInsert.Parameters.AddWithValue("@SL", tbSL.Text);
                cmdInsert.Parameters.AddWithValue("@RLL", tbRLL.Text);
                cmdInsert.Parameters.AddWithValue("@JJ", tbJJ.Text);
                cmdInsert.Parameters.AddWithValue("@PJ", tbPJ.Text);
                cmdInsert.Parameters.AddWithValue("@LSJ", tbLSJ.Text);
                cmdInsert.Parameters.AddWithValue("@GYL", tbGYL.Text);
                //---
                //cmdInsert.Parameters.AddWithValue("@FPRQ", System.DateTime.Now.Ticks);
                //cmdInsert.Parameters.AddWithValue("@FPH", tbFPH.Text);
                //cmdInsert.Parameters.AddWithValue("@SCRQ", dtpSCRQ.Value.Ticks);
                //cmdInsert.Parameters.AddWithValue("@SCSBM", cbSCCJ.Text);
                //cmdInsert.Parameters.AddWithValue("@YXRQ", dtpYXRQ.Value.Ticks);
                //cmdInsert.Parameters.AddWithValue("@ZCSB", tbZCSB.Text);
                //---
                cmdInsert.Parameters.AddWithValue("@YSJL", tbSL.Text);
                cmdInsert.Parameters.AddWithValue("@HWBM", tbHW.Text);
                cmdInsert.Parameters.AddWithValue("@SCXKZ", tbSCXK.Text);
                cmdInsert.Parameters.AddWithValue("@WSXKZ", tbWSXK.Text);
                //---
                cmdInsert.Parameters.AddWithValue("@GYSBM", tbGYS.Text);
                cmdInsert.Parameters.AddWithValue("@BZ", tbBZ.Text);
                //cmdInsert.Parameters.AddWithValue("@YSRQ", System.DateTime.Now.Ticks);
                cmdInsert.Parameters.AddWithValue("@YPKC", tbSL.Text);
                //---
                cmdInsert.ExecuteNonQuery();
                MessageBox.Show("保存成功！");
                con.Close();

                //sda = new SqlDataAdapter("SELECT * FROM ML_RKD1",con);
                //ds = new DataSet();
                //sda.Fill(ds, "RKDMX");
                //builder = new SqlCommandBuilder(sda);
                //DataRow dr = ds.Tables["RKDMX"].NewRow();
                //dr["RKDH"] = rkd;

                //SqlCommand cmd = new SqlCommand ("select * from ml_rdk1",con);
                //SqlDataReader sdr = cmd.ExecuteReader();
                //tbBM.Text = sdr["BM"].ToString();

                //JE,JJJE,PJJE,LJJE,FKRQ,SYSL,FLAG,YSRY,) 
                //VALUES(";
                //YFJE,CKLSJ,AUTORKID,LYRKID,ZYDJ,ZYCD,WKZL,BZZL,HSVEBQ,HAVESMS,BZSL,BGY没有写
                //sSave += rkd + ",'";
                //sSave += int.Parse(tbBM.Text) + "','";
                //sSave += dtpSCRQ.Value.Ticks + "','";
                //sSave += dtpYXRQ.Value.Ticks + "','";
                //sSave += tbSCS.Text + "','";
                //sSave += tbPH.Text + "','";
                //sSave += tbRLL.Text + "','";
                //sSave += tbGYL.Text + "','";
                //sSave += tbJJ.Text + "','";
                //sSave += tbPJ.Text + "','";
                //sSave += tbLSJ.Text + "','";
                //sSave += tbSL.Text + "','";
                ////sSave += tbSL.Text + "','";
                //sSave += int.Parse(tbJJ.Text) * int.Parse(tbSL.Text) + "','";
                //sSave += tbJJJE.Text + "','";
                //sSave += tbPJJE.Text + "','";
                //sSave += tbLSJE.Text + "','";
                //sSave += tbFPH.Text + "','";
                //sSave += dtpFPRQ.Value.Ticks + "','";
                //sSave += System.DateTime.Now.Ticks + "','";
                //sSave += tbSL.Text + "','";
                //sSave += tbHW.Text + "',";
                //sSave += "0,'";
                //sSave += tbZCSB.Text + "','";
                //sSave += tbWH.Text + "','";
                //sSave += tbGYS.Text + "','";
                //sSave += tbBZ.Text + "','";
                //sSave += tbYSJL.Text + "','";
                //sSave += tbCGY.Text + "','";
                //sSave += System.DateTime.Now.Ticks + "','";
                //sSave += tbSL.Text + "','";
                //sSave += tbWSXK.Text + "','";
                //sSave += tbSCXK.Text + "')";
                //con = new SqlConnection(PublicUnit.ConnetionString);
                //con.Open();
                //SqlCommand scmd = new SqlCommand(sSave, con);
                //scmd.ExecuteNonQuery();
                //con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void tbBM_Validated(object sender, EventArgs e)
        {
            string sFH = "SELECT MC,JLDW FROM Dic_YP";
            sda = new SqlDataAdapter(sFH,con);
            ds = new DataSet();
            sda.Fill(ds);
        }

        private void bindingNavigatorCancelItem_Click(object sender, EventArgs e)
        {
            tbBM.Text = "";
            tbMC.Text = "";
            tbJL.Text = "";
            tbYPBZ.Text = "";
            tbPH.Text = "";
            cbWH.Text = "";
            tbSL.Text = "";
            tbRLL.Text = "";
            tbJJ.Text = "";
            tbPJ.Text = "";
            tbLSJ.Text = "";
            tbGYL.Text = "";
            tbDQLJ.Text = "";
            tbBPLJ.Text = "";
            tbJJJE.Text = "";
            tbPJJE.Text = "";
            tbLSJE.Text = "";
            tbJLC.Text = "";
            tbFPH.Text = "";
            cbSCCJ.Text = "";
            tbZCSB.Text = "";
            tbWGYS.Text = "";
            tbBZYS.Text = "";
            tbYSJL.Text = "";
            tbHW.Text = "";
            tbSCXK.Text = "";
            tbWSXK.Text = "";
            dgvYPBM.Visible = false;
        }

        private void tbBM_TextChanged(object sender, EventArgs e)
        {
            string ZJF = tbBM.Text.Trim();
            string sqlstr;
            if (Regex.IsMatch(tbBM.Text, "[0-9]{1,10}"))
            {
                sqlstr = "SELECT MC AS '药品名称',BM AS '药品编码' FROM Dic_YP WHERE BM LIKE '" + ZJF + "%'";
            }
            else if (Regex.IsMatch(tbBM.Text, "^[a-zA-Z]+$"))
            {
                sqlstr = "SELECT MC AS '药品名称',BM AS '药品编码' FROM Dic_YP WHERE ZJF LIKE '" + ZJF + "%'";
            }
            else
            {
                sqlstr = "SELECT MC AS '药品名称',BM AS '药品编码' FROM Dic_YP WHERE MC LIKE '" + ZJF + "%'";
            }
            DataTable dtload = new DataTable();
            dgvYPBM.Visible = true;
            sda = new SqlDataAdapter(sqlstr, con);
            DataSet ds1 = new DataSet ();
            sda.Fill (ds1);
            dgvYPBM.DataSource = ds1.Tables[0];
        }

        private void tbBM_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                dgvYPBM.Focus();
                dgvYPBM.Visible = false;
            }

            if (e.KeyCode == Keys.Down)
            {
                dgvYPBM.Focus();
                dgvYPBM.CurrentRow.Selected = true;
            }

            if (e.KeyCode == Keys.Enter)
            {
                int z = dgvYPBM.CurrentRow.Index;
                this.tbMC.Text = this.dgvYPBM.Rows[z].Cells[0].Value.ToString();
                this.tbBM.Text = this.dgvYPBM.Rows[z].Cells[1].Value.ToString();
                try
                {
                    string sGetInformation = "SELECT JLDW FROM Dic_YP WHERE BM = '" +
                        tbBM.Text + "'";
                    con = new SqlConnection(PublicUnit.ConnetionString);
                    con.Open();
                    SqlCommand cmdGetInformation = new SqlCommand(sGetInformation, con);
                    SqlDataReader sqldr = cmdGetInformation.ExecuteReader();
                    sqldr.Read();
                    tbJL.Text = sqldr[0].ToString();
                    sqldr.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                this.dgvYPBM.Visible = false;
                tbYPBZ.Focus();
            }

        }

        private void dgvYPBM_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                int z = dgvYPBM.CurrentRow.Index;
                this.tbMC.Text = this.dgvYPBM.Rows[z].Cells[0].Value.ToString();
                this.tbBM.Text = this.dgvYPBM.Rows[z].Cells[1].Value.ToString();
                try
                {
                    string sGetInformation = "SELECT JLDW FROM Dic_YP WHERE BM = '" +
                        tbBM.Text + "'";
                    con = new SqlConnection(PublicUnit.ConnetionString);
                    con.Open();
                    SqlCommand cmdGetInformation = new SqlCommand(sGetInformation, con);
                    SqlDataReader sqldr = cmdGetInformation.ExecuteReader();
                    sqldr.Read();
                    tbJL.Text = sqldr[0].ToString();
                    sqldr.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                this.dgvYPBM.Visible = false;
                tbYPBZ.Focus();
            }
        }

        private void bindingNavigatorRefreshItem_Click(object sender, EventArgs e)
        {
            tbDH.Text = Convert.ToString(rkd);
            con = new SqlConnection(PublicUnit.ConnetionString);
            string sRefresh = "SELECT YPBM,(SELECT YP.MC FROM Dic_YP YP,ML_RKD1 RKD1 WHERE RKD1.YPBM = YP.BM AND RKD1.RKDH = " +
                rkd + "),BZSL,FLAG,SL,JJ,PJ,LSJ,PH,FPH FROM ML_RKD1 WHERE RKDH = " + rkd;
            SqlDataAdapter sdaRefresh = new SqlDataAdapter(sRefresh, con);
            SqlCommandBuilder builderRefresh = new SqlCommandBuilder(sdaRefresh);
            DataSet dsRefresh = new DataSet();
            BindingSource bsRefresh = new BindingSource();
            dgvYPRKD.AutoGenerateColumns = true;
            try
            {
                sdaRefresh.Fill(dsRefresh);
                dgvYPRKD.DataSource = dsRefresh.Tables[0];
                dgvYPRKD.Columns[0].HeaderText = "编码";
                dgvYPRKD.Columns[1].HeaderText = "名称";
                dgvYPRKD.Columns[2].HeaderText = "剂量";
                dgvYPRKD.Columns[3].HeaderText = "包装";
                dgvYPRKD.Columns[4].HeaderText = "数量";
                dgvYPRKD.Columns[5].HeaderText = "进价";
                dgvYPRKD.Columns[6].HeaderText = "批价";
                dgvYPRKD.Columns[7].HeaderText = "零价";
                dgvYPRKD.Columns[8].HeaderText = "批号";
                dgvYPRKD.Columns[9].HeaderText = "发票号";
                bsRefresh.DataSource = dsRefresh.Tables[0];
                bNavigator.BindingSource = bsRefresh;
                dgvYPRKD.Rows[0].Selected = true;
            }
            catch (SqlException ex)
            {
                if (ex.Number == 18456)
                    MessageBox.Show("网络故障，请稍后再试！", "警告" + ex.Number, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void tbJJ_TextChanged(object sender, EventArgs e)
        {
            if(tbSL.Text != "")
                tbJJJE.Text = (Convert.ToInt32(tbJJ.Text) * Convert.ToInt32(tbSL.Text)).ToString();
        }

        private void tbSL_TextChanged(object sender, EventArgs e)
        {
            if(tbJJ.Text != "")
                tbJJJE.Text = (Convert.ToInt32(tbJJ.Text) * Convert.ToInt32(tbSL.Text)).ToString();
            if(tbLSJ.Text != "")
                tbLSJE.Text = (Convert.ToInt32(tbLSJ.Text) * Convert.ToInt32(tbSL.Text)).ToString();
        }

        private void tbLSJ_TextChanged(object sender, EventArgs e)
        {
            if(tbSL.Text != "")
                tbJJJE.Text = (Convert.ToInt32(tbLSJ.Text) * Convert.ToInt32(tbSL.Text)).ToString();
        }
    }
}
